package com.heima.wemedia.controller;

import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.model.admin.dtos.NewsAuthDto;
import com.heima.model.wemedia.dto.WmNewsPageDTO;
import com.heima.model.media.dtos.WmNewsDownOrUpDTO;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.model.media.dtos.WmNewsPageReqDto;
import com.heima.wemedia.entity.WmNews;
import com.heima.wemedia.entity.WmNewsPageVO;
import com.heima.wemedia.service.WmNewsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class WmNewsController {

    @Autowired
    private WmNewsService newsService;

    @PostMapping("/api/v1/news/page")
    public PageResult<WmNews> page(@RequestBody WmNewsPageReqDto dto) {
        return newsService.findByPage(dto);
    }

    /**
     * 自媒体用户登录后，新增、修改、发布自媒体文章
     */
    @PostMapping("/api/v1/news/submit")
    public ResponseResult submit(@RequestBody WmNewsDto dto) {
        newsService.submit(dto);
        return ResponseResult.ok();
    }

    /**
     * 自媒体用户登录后，根据主键id查询自媒体文章
     *
     * @param id 自媒体文章id
     */
    @ApiOperation("根据id 查询自媒体文章")
    @GetMapping("/api/v1/news/one/{id}")
    public ResponseResult<WmNews> findNewsById(@PathVariable("id") Integer id) {
        WmNews wmNews = newsService.getById(id);
        if (wmNews == null) {
            return ResponseResult.error(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.ok(wmNews);
    }

    /**
     * 自媒体文章上下架
     */
    @PutMapping("/api/v1/news/down_or_up")
    public ResponseResult downOrUp(@Valid @RequestBody WmNewsDownOrUpDTO dto) {
        newsService.downOrUp(dto);
        return ResponseResult.ok();
    }

    /**
     * 自媒体用户登录后，删除自媒体文章，如果已经通过审核发布的文章不能删除
     *
     * @param id 自媒体文章id
     */
    @ApiOperation("根据id 删除自媒体文章")
    @DeleteMapping("/api/v1/news/del_news/{id}")
    public ResponseResult delNews(@PathVariable("id") Integer id) {
        newsService.deleteById(id);
        return ResponseResult.ok();
    }

    /**
     * 根据id查询文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/api/v1/news_auth/one/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id) {
        return ResponseResult.ok(newsService.findById(id));
    }

    /**
     * feign调用文章列表查询
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news/list")
    public List<WmNewsPageVO> authList(@RequestBody WmNewsPageDTO dto){
        return newsService.listByTitle(dto);
    }

    /**
     * feign调用文章人工审核成功
     * @param newsId
     * @return
     */
    @PostMapping("/api/v1/news/auth_pass")
    public WmNews authPass(@RequestParam("id")Integer newsId){
        return newsService.authPass(newsId);
    }

    /**
     * feign调用文章人工审核失败
     */
    @PostMapping("/api/v1/news_auth/auth_fail")
    public void authFail(@RequestBody NewsAuthDto dto){
        newsService.authFail(dto);
    }
}
